< previous page page_404 next page >

Page 404
The File, Open (Index = 2) code in Listing 17.8 simply displays an Open dialog box so the user can supply a file name. If the user doesn't click Cancel, then the file is opened using the OpenFile routine, as presented in Listing 17.8. The last item in the select statement is Index 4, which represents the File, Exit case. This is an easy one because the proper way to terminate an MDI application is to unload the MDI form.
As mentioned earlier, the OpenFile code is responsible for opening a text file and loading it into a text box on your child form. This code is very simplistic and includes no basic error handling for such cases as testing for files greater than 44K under Windows 95. However, it does provide a basic example of how to load a file into a text box, which is sufficient for this example.
CAUTION
Avoid using the End statement to terminate your applications. End terminates your application immediately, which prevents your Form_Unload events from being executed. The best way to end an MDI application is to unload the MDI form.

Listing 17.8 MDIPARENT3.TXTShared Code
'*********************************************************************
' Code shared among the child windows should be put in either a
' module or the MDI parent form.  This OpenFile code will be used
' by all of the children, so we will keep it in the MDI parent form.
'*********************************************************************
Public Sub OpenFile(strFileName As String)
    Dim strFileContents As String
    Dim intFileNum As Integer
    '*****************************************************************
    ' Get a free file handle
    '*****************************************************************
    intFileNum = FreeFile
    '*****************************************************************
    ' Open the file
    '*****************************************************************
    Open strFileName For Input As intFileNum
        '*************************************************************
        ' Put the contents of the file into the txtData control of
        ' the child form. This code will fail if the file is too
        ' large to fit in the textbox, so you should include
        ' additional error handling in your own code.
        '*************************************************************
        With ActiveForm
            .txtData.Text = Input$(LOF(intFileNum), intFileNum)
            '********************************************************
            ' Set the caption of the child form to the filename
            '********************************************************
            .Caption = strFileName
        End With
    '*****************************************************************
    ' Always close files you open as soon as you are done with them

 
< previous page page_404 next page >